import json
from pathlib import Path
from typing import Dict
from metagpt.logs import logger

class ReviewCounter:
    """A singleton class to manage review counts and versions"""
    _instance = None
    _count_data: Dict[str, Dict[str, int]] = {}  # 修改为嵌套字典，记录每个版本的计数
    
    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
        return cls._instance
    
    @classmethod
    def get_version(cls, filename: str) -> int:
        """Get current version number for a file"""
        if filename not in cls._count_data:
            cls._count_data[filename] = {}
            return 1
        return len(cls._count_data[filename]) + 1
    
    @classmethod
    def increment_count(cls, filename: str) -> tuple[int, int]:
        """Increment review count and return (version, count)"""
        version = cls.get_version(filename)
        
        if f"v{version}" not in cls._count_data[filename]:
            cls._count_data[filename][f"v{version}"] = 0
        cls._count_data[filename][f"v{version}"] = 1
        
        return version, cls._count_data[filename][f"v{version}"]
    
    @classmethod
    def get_count(cls, filename: str, version: int) -> int:
        """Get current review count for a specific version of a file"""
        if filename not in cls._count_data:
            cls._count_data[filename] = {}
        version_key = f"v{version}"
        return cls._count_data[filename].get(version_key, 0)
    
    @classmethod
    def reset(cls):
        """Reset all counts"""
        cls._count_data = {}